﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Rob Ashton 2009
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@mvcex.com
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace MvcEx.Tests
{
    public static class TestHelper
    {
        public static Object GetField(object instance, String name)
        {
            Type t = instance.GetType();

            FieldInfo f = t.GetField(name, BindingFlags.Instance
                                   | BindingFlags.NonPublic
                                   | BindingFlags.Public);

            return f.GetValue(instance);
        }

        public static Object CallMethod(Object instance, String methodName, params Object[] args)
        {
            Type t = instance.GetType();
            List<Type> argTypes = new List<Type>();
            foreach (Object arg in args)
            {
                argTypes.Add(arg.GetType());
            }

            MethodInfo methodInfo = t.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, argTypes.ToArray(), null);
            return methodInfo.Invoke(instance, args);
        }
        
        public static string MapPath(String testName, string path)
        {
            String relativePath = string.Format("data/{0}", testName);

            if (path.StartsWith("~"))
            {
                return path.Replace("~", relativePath);
            }
            else if (path.StartsWith("/") || path.StartsWith("\\"))
            {
                return relativePath + path;
            }
            else
            {
                return path;
            }
        }
    }
}
